﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Continuum.Classes;

namespace Continuum
{
    public partial class EditMarketFunds : Form
    {
        public enum CurrentFormStateEnum { View, Add, Edit };
        public CurrentFormStateEnum CurrentFormState;

        public EditMarketFunds()
        {
            InitializeComponent();
        }

        private void EditMarketFunds_Load(object sender, EventArgs e)
        {
            SetFormState(CurrentFormStateEnum.View);
            lstFunds.DataSource = MainForm.market.MarketFunds;
        }

        private void SetFormState(CurrentFormStateEnum fse)
        {
            CurrentFormState = fse;

            if (fse == CurrentFormStateEnum.View)
            {
                txtFundName.Enabled = false;
                numQuantity.Enabled = false;
                txtMarketSymbol.Enabled = false;
                dtpInceptionDate.Enabled = false;
                chkBenchmark.Enabled = false;
                chkHidden.Enabled = false;
            }

            if (fse == CurrentFormStateEnum.Add)
            {
                ClearForm();

                txtFundName.Enabled = true;
                numQuantity.Enabled = true;
                txtMarketSymbol.Enabled = true;
                dtpInceptionDate.Enabled = true;
                chkBenchmark.Enabled = true;
                chkHidden.Enabled = true;
            }

            if (fse == CurrentFormStateEnum.Edit)
            {
                txtFundName.Enabled = true;
                numQuantity.Enabled = true;
                txtMarketSymbol.Enabled = true;
                dtpInceptionDate.Enabled = true;
                chkBenchmark.Enabled = true;
                chkHidden.Enabled = true;
            }

        }

        private void ClearForm()
        {
            txtFundName.Text = "Sample MarketFund";
            numQuantity.Value= 0;
            txtMarketSymbol.Text = "YHOO";
            dtpInceptionDate.Value = DateTime.Now.Date.AddMonths(-6);
            chkBenchmark.Checked = false;
            chkHidden.Checked = false;
        }

        private void lstFunds_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstFunds.SelectedItem == null) return;

            ClearForm();

            MarketFund marketfund = (MarketFund)lstFunds.SelectedItem;

            txtFundName.Text = marketfund.Name;
            if (marketfund.InceptionDate != DateTime.MinValue)
                dtpInceptionDate.Value = marketfund.InceptionDate;
            txtMarketSymbol.Text = marketfund.MarketSymbol;
            numQuantity.Value = marketfund.Quantity;
            chkBenchmark.Checked = marketfund.IsBenchmark;
            chkHidden.Checked = marketfund.IsHidden;

            SetFormState(CurrentFormStateEnum.View);
        }

        private void btnNewFund_Click(object sender, EventArgs e)
        {
            SetFormState(CurrentFormStateEnum.Add);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            SetFormState(CurrentFormStateEnum.Edit);
        }

        private bool ValidateForm()
        {
            string ErrorList = "";

            txtFundName.Text = txtFundName.Text.Trim();
            if (txtFundName.Text == "")
                ErrorList += "- You need to specify a valid Fund Name\n";

            txtMarketSymbol.Text = txtMarketSymbol.Text.Trim();
            if (txtFundName.Text == "")
                ErrorList += "- You need to specify a valid Market Symbol\n";

            if (numQuantity.Value == 0) 
                ErrorList += "- You need to enter a valid Quantity\n";

            if (ErrorList == "") return true;

            MessageBox.Show("Please correct the following errors :\n" + ErrorList, "Unable to save market fund", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateForm()) return;

            DateTime InceptionDate = dtpInceptionDate.Value;

            MarketFund marketfund;

            if (CurrentFormState == CurrentFormStateEnum.Add)
            {
                marketfund = new MarketFund(InceptionDate, txtFundName.Text, txtMarketSymbol.Text, (int)numQuantity.Value, chkBenchmark.Checked, chkHidden.Checked);
                MainForm.market.MarketFunds.Add(marketfund);
            }
            else
            {
                marketfund = (MarketFund)lstFunds.SelectedItem;
                marketfund.Name = txtFundName.Text;
                marketfund.InceptionDate = dtpInceptionDate.Value.Date;
                marketfund.Quantity = (int) numQuantity.Value;
                marketfund.MarketSymbol = txtMarketSymbol.Text;
                marketfund.IsBenchmark = chkBenchmark.Checked;
                marketfund.IsHidden = chkHidden.Checked;
            }

            SetFormState(CurrentFormStateEnum.View);

            // Refresh List bindings
            lstFunds.DataSource = null;
            lstFunds.DataSource = MainForm.market.MarketFunds;
        }

        private void btnDeleteFund_Click(object sender, EventArgs e)
        {
            if (lstFunds.SelectedItem == null) return;

            MarketFund marketfund = (MarketFund) lstFunds.SelectedItem;

            if (MessageBox.Show("Are you sure you want to delete market fund " + marketfund.Name + "?", "Confirm fund deletion", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK) return;

            MainForm.market.MarketFunds.Remove(marketfund);

            // Refresh List bindings
            lstFunds.DataSource = null;
            lstFunds.DataSource = MainForm.market.MarketFunds;
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void lnkOnlineHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.UseShellExecute = true;
            proc.StartInfo.FileName = "http://project-continuum.com/OnlineHelp/EditMarketFunds.aspx";
            proc.Start();

        }
    }
}
